package ice;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import omero.ServerError;
import cli.Algorithms;
import cli.Logging;
import cli.OmeroServer;
import cli.Settings;
import exception.DataException;
import exception.MatlabException;
import exception.ServerConnectionException;
import Ice.Current;
import actions.Indexing;
import actions.Searching;

public class MipsI  extends Spike._MipsDisp {

	DateFormat dateFormat = new SimpleDateFormat( Settings.DATE_FORMAT );
	
	Algorithms algorithm = new Algorithms();
	
	/**
	 * Called by the server to start the search action on the omero server
	 *
	 * @param host
	 * @param username
	 * @param password
	 * @param algo 
	 * @param datasetId
	 * @param roiId
	 * 
	 * @return error indication 0 = no errors
	 */
	public int doSearch( String host, String username, String password, String algo, long datasetId, long roiId, Ice.Current current ) {
		
		int status = 0;
		
		String msg = dateFormat.format( new Date() ) + " Start searching for  dataset: " +  datasetId + ", roiId + " + roiId;
		System.out.println( "\n" + msg );
		Logging.log("System", msg + "\n");
//		Server.logMessageToScreen( msg );
		try {
			OmeroServer.open( host, 4064, username, password );
			Searching.start( roiId, datasetId, algo );
			OmeroServer.close();
		} catch (ServerConnectionException e) {
			Logging.log( "Error", e.getMessage() );
			status = -1;
			Server.setLastErrorMessage( e.getMessage() );
			OmeroServer.close();
		} catch (ServerError e) {
			Logging.log( "Error", e.getMessage() );
			status  = -1;
			Server.setLastErrorMessage( e.getMessage() );
			OmeroServer.close();
		} catch (IOException e) {
			Logging.log( "Error", e.getMessage() );
			status  = -1;
			Server.setLastErrorMessage( e.getMessage() );
			OmeroServer.close();
		} catch (NoSuchAlgorithmException e) {
			Logging.log( "Error", e.getMessage() );
			status  = -1;
			Server.setLastErrorMessage( e.getMessage() );
			OmeroServer.close();
		} catch (DataException e ) {
			Logging.log( "Error", e.getMessage() );
			status  = -1;
			Server.setLastErrorMessage( e.getMessage() );
			OmeroServer.close();
		} catch (Exception e ) {
			Logging.log( "Error", e.getMessage() );
			status  = -1;
			Server.setLastErrorMessage( e.getMessage() );
			OmeroServer.close();
		}
		return status;
	}
	

	public int doIndex( String host, String username, String password, long datasetId,  Ice.Current current) {
		int status = 0;
		String msg = dateFormat.format( new Date() ) + " Start indexing dataset: " +  datasetId;
		System.out.println( "\n" + msg );
		Logging.log("System", " Start indexing dataset: " +  datasetId);
//		Server.logMessageToScreen( msg );
		try {
			OmeroServer.open( host, 4064, username, password );
			Indexing.start( datasetId );
		} catch (ServerConnectionException e) {
			Logging.log( "Error", e.getMessage() );
			status = -1;
			Server.setLastErrorMessage( e.getMessage() );
			OmeroServer.close();
		} catch (ServerError e) {
			Logging.log( "Error", e.getMessage() );
			status  = -1;
			Server.setLastErrorMessage( e.getMessage() );
			OmeroServer.close();
		} catch (IOException e) {
			Logging.log( "Error", e.getMessage() );
			status  = -1;
			Server.setLastErrorMessage( e.getMessage() );
			OmeroServer.close();
		} catch (NoSuchAlgorithmException e) {
			Logging.log( "Error", e.getMessage() );
			status  = -1;
			Server.setLastErrorMessage( e.getMessage() );
			OmeroServer.close();
		} catch (DataException e ) {
			Logging.log( "Error", e.getMessage() );
			status  = -1;
			Server.setLastErrorMessage( e.getMessage() );
			OmeroServer.close();
		} catch ( MatlabException e ) {
			Logging.log( "Error", e.getMessage() );
			status  = -1;
			Server.setLastErrorMessage( e.getMessage() );
			OmeroServer.close();
		}
		return status;
	}

	public String[] getAlgoList( Ice.Current current) {
		String msg = dateFormat.format( new Date() ) +  " Retrieving matlab algorithms...";
		System.out.print( "\n" + msg );
		Logging.log("System",  msg );
//		Server.logMessageToScreen( msg );
		algorithm.obtainAlgorithmList();
		
		System.out.println(" Done.");
		
		return algorithm.getAlgorithmList();

	}
	
	public String getLastErrorMessage( Ice.Current current )  {
		
		return Server.getLastErrorMessage();
	}

}
